function oz_redshift2index,z,aexp=aexp,snipshot=snipshot,snaplist=snaplist,verbose=verbose

;+
; NAME:
;       OZ_REDSHIFT2INDEX_BLAH
;
; PURPOSE:
;       Searches through ascii list of expansion factors to find that
;       closest to a prescribed redshift or expansion factor
;
; CALLING SEQUENCE:
;       OZ_REDSHIFT2INDEX(Z[,AEXP=AEXP,SNIPSHOT=SNIPSHOT,SNAPLIST=SNAPLIST,SNIPLIST=SNIPLIST])
;
; INPUTS:
;       Z:         The desired redshift for which to return the
;       snapshot number
;
; OPTIONAL INPUTS:
;       AEXP:      If set (aexp=1) the z input is treated as an expansion
;       factor instead (so the simple conversion need not be performed in the
;       driver script)
;
;       SNIPSHOT:  Script will search through the default OWLS_ZOOMS
;       snipshot list instead of the bluegene snapshots, and return
;       the appropriate snipshot index.
;
;       SNAPLIST: selects an alternative list of snapshots (SNIPSHOT
;       option is then ignored if set)
;
; OUTPUTS:
;       The integer index into the list (starting from zero)
;
; MODIFICATION HISTORY:
;       Started by Rob Crain, Swinburne, 24-Feb-2010
;-

; are we looking for a redshift or expansion factor
if keyword_set(aexp) then begin
   if keyword_set(verbose) then print,'[oz_redshift2index] treating input as an expansion factor'
   a = z
endif else a = 1.0 / (1.0+z)

; set default paths
if keyword_set(snaplist) then begin
   if keyword_set(verbose) then begin
      if keyword_set(snipshot) then print,'[oz_redshift2index] ignoring snipshot keyword'
      print,'[oz_redshift2index] using supplied output list'
      print,'[oz_redshift2index]  '+snaplist
      list = snaplist
   endif
endif else begin
   if keyword_set(snipshot) then begin
      if keyword_set(verbose) then print,'[oz_redshift2index] using snipshot output list'
      list = '/nfs/cluster/galaxy4/rcrain/OWLS_ZOOMS/snipshot_outputs.txt'
   endif else begin
      if keyword_set(verbose) then print,'[oz_redshift2index] using bluegene output list'
      list = '/nfs/cluster/galaxy4/rcrain/OWLS_ZOOMS/bluegene_outputs.txt'
   endelse
endelse

; check list exists
exists = file_test(list)
if not(exists) then begin
   if keyword_set(verbose) then print,'[oz_redshift2index] list not found'
   return,-1
endif

; read list
readcol,list,alist,/silent
nlist = n_elements(alist)
cnlist = strtrim(string(nlist),2)
if keyword_set(verbose) then print,'[oz_redshift2index] found '+cnlist+' expansion factors in list'

; find nearest
isnap = value_locate(alist,a)

; value_locate finds only the *interval*, not the nearest, so isnap+1
; might be nearer
if(isnap+1 LT nlist)then begin
   dt_low  = a - alist[isnap]
   dt_high = alist[isnap+1] - a
   if(dt_high LT dt_low) then isnap += 1
endif

return,isnap

end
